export default {
	namespaced:true,
	state(){
		return {
			cartList:[],
			popShow:false,
			popData:null,  //弹窗组件数据
			editIdx:-1  //-1新增，其他正常序号代表修改
		}
	},
	getters:{
		allInfo(state){
			let allPrice = 0
			let allCheck = true //全选状态
			state.cartList.forEach(item=>{
				let {list,dropIdx,num,ischeck} = item
				allPrice += list[dropIdx].price * num
				if(!ischeck){
					allCheck = false
				}
			})
			console.log('getters',state.cartList);
			uni.setStorage({
				key:'uni-cart-2301',
				data:state.cartList
			})
			return {
				allPrice,
				allCheck
			}
		}
	},
	mutations:{
		initCartMut(state,list){
			state.cartList = list
		},
		cartAddMut(state,cakeObj){
			let {objectId,dropIdx,num} = cakeObj
			let {cartList,editIdx} = state
			// 判断是否为编辑商品
			if(editIdx!=-1){
				cartList[editIdx] = cakeObj
				state.cartList = [...cartList]
				return
			}
			//判断购物车中的同类商品
			for(let i=0;i<cartList.length;i++){
				if(cartList[i].objectId==objectId&&cartList[i].dropIdx==dropIdx){
					cartList[i].num += num
					return
				}
			}
			//新增新商品
			cakeObj.ischeck = true
			state.cartList.push(cakeObj)
		},
		popShowMut(state,bool){
			state.popShow = bool
		},
		popDataMut(state,cakeObj){
			state.popData = cakeObj
		},
		//区分新增编辑
		editIdxMut(state,idx){
			state.editIdx = idx
		},
		//单选
		cartCheckMut(state,idx){
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck
		},
		//全选
		cartAllCheckMut(state,bool){ 
			state.cartList.forEach(item=>{
				item.ischeck = !bool
			})
		}
	}
}